package com.fcgzs.question.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fcgzs.common.utils.PageUtils;
import com.fcgzs.common.utils.Query;

import com.fcgzs.question.dao.QuestionDao;
import com.fcgzs.question.entity.QuestionEntity;
import com.fcgzs.question.service.QuestionService;
import org.springframework.web.multipart.MultipartFile;


@Service("questionService")
public class QuestionServiceImpl extends ServiceImpl<QuestionDao, QuestionEntity> implements QuestionService {

    @Autowired
    private QuestionDao questionDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty((String)params.get("key"))) {
            queryWrapper.eq("id",(String)params.get("key")).or().like("title",(String)params.get("key"));
        }


        IPage<QuestionEntity> page = this.page(
                new Query<QuestionEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String,Object> importExcel(MultipartFile multipartFile) {
        Map resultMap = new HashMap();
        //文件全名   工资表.xlsx
        String filename = multipartFile.getOriginalFilename();
        String extName = filename.substring(filename.lastIndexOf(".") + 1);
        if(!extName.equals("xls")&&!extName.equals("xlsx")){
            resultMap.put("result",false);
            resultMap.put("msg","文件格式不正确，请重新上传");
            return resultMap;
        }
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(multipartFile.getInputStream());
            XSSFSheet sheet = workbook.getSheetAt(0);
            int rows = sheet.getPhysicalNumberOfRows();//含有数据的行数
            for (int i = 1; i < rows; i++) {
                XSSFRow row = sheet.getRow(i);
                QuestionEntity questionEntity = new QuestionEntity();
                questionEntity.setTitle(row.getCell(0).getStringCellValue());     //题目标题
                questionEntity.setAnswer(row.getCell(1).getStringCellValue());    //解答
                questionEntity.setLevel((int) row.getCell(2).getNumericCellValue());  //难度等级
                questionEntity.setDisplayOrder((int)row.getCell(3).getNumericCellValue());   //排序
                questionEntity.setSubTitle(row.getCell(4).getStringCellValue());     //副标题
                questionEntity.setType((long)row.getCell(5).getNumericCellValue());   //类型
                questionEntity.setEnable((int)row.getCell(6).getNumericCellValue());    //是否显示
                //执行保存
                this.save(questionEntity);

            }
            resultMap.put("result",true);
            resultMap.put("msg","保存成功");
            resultMap.put("num",rows-1);
            return resultMap;
        } catch (IOException e) {
            e.printStackTrace();
            resultMap.put("result",false);
            resultMap.put("msg","保存失败");
            return resultMap;
        }
    }

    /**
     * 导出Excel数据
     *
     * @return
     */
    @Override
    public Workbook exportExcel() {
        //1.查询数据
        List<QuestionEntity> list = this.list();

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("题库列表");

        XSSFRow row0 = sheet.createRow(0);
        XSSFCell row0Cell0 = row0.createCell(0);
        row0Cell0.setCellValue("ID");
        XSSFCell row0Cell1 = row0.createCell(1);
        row0Cell1.setCellValue("标题");

        //2.遍历数据，构建工作簿对象
        for (int i = 0; i < list.size(); i++) {
            XSSFRow row = sheet.createRow(i+1);
            XSSFCell rowCell0 = row.createCell(0);
            rowCell0.setCellValue(list.get(i).getId());
            XSSFCell rowCell1 = row.createCell(1);
            rowCell1.setCellValue(list.get(i).getTitle());
        }
        return workbook;
    }

    /**
     * 查询分类下的题目数量
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> countTypeQuestion() {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("TYPE as name,count(*) as num").groupBy("TYPE");
        List<Map<String, Object>> mapList = questionDao.selectMaps(queryWrapper);
        return mapList;
    }

}